package appfight.map;

import java.util.ArrayList;
import java.util.List;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;

import android.util.Log;
import appfight.level.GameLevel;

public class GameMap {

	public enum MapBlockIndex
	{
		black,
		grass,
		room_floor,
		room_floor2,
		trees,
		wood_wall_verticle,
		wood_wall_horizontal,
		town_floor,
		connect_room_floor
	}
	
	MapBlockInformation[] MapBlockDictionary= 
	{                           //picstring    blockable
		new MapBlockInformation("black.png"      ,false),
		new MapBlockInformation("grass.png"      ,false),
		new MapBlockInformation("room_floor.png" ,false),
		new MapBlockInformation("room_floor2.png",false),
		new MapBlockInformation("trees.png"      ,true),
		new MapBlockInformation("wood_wall.png"  ,true),
		new MapBlockInformation("wood_wall2.png" ,true),
		new MapBlockInformation("town_floor.png" ,false),
		new MapBlockInformation("room_floor3.png" ,false),
		
		
	};
	
	
	public int [][] _mapData;		
	public Sprite [][] _sprite;
	public boolean [][] _blockMap;	
	public int [][] _AStarTable;
	
	public static final int INFINITE = 999;
	public MapClickedEventHandler _mapClicked;
	
	private GameLevel _level;
	
	public class MapBlockInformation
	{
		boolean _isBlock;
		String _picString;
		
		public MapBlockInformation(String picString, boolean isBlock)
		{
			_picString = picString;
			_isBlock = isBlock;
		}
	}
	
	
	public int GetSizeX()
	{
		return _mapData[0].length;		
	}
	
	public int GetSizeY()
	{
		return _mapData.length;		
	}
	
	public void UpdateAStarTable(int targetX,int targetY)
	{
		if(_AStarTable[targetY][targetX]==0)return;
		
		
		
		int sizeX = GetSizeX();
		int sizeY = GetSizeY();
		
		for(int i=0;i<sizeX;i++)
			for(int j=0;j<sizeY;j++)
			{
				_AStarTable[j][i]=INFINITE;
			}
		
		_AStarTable[targetY][targetX]=0;
		
		List<Integer> listX = new ArrayList<Integer>();
		List<Integer> listY = new ArrayList<Integer>();
		
		listX.add(targetX);
		listY.add(targetY);
		
		while(listX.size()>0||listY.size()>0)
		{
			int nowX = listX.get(0);
			int nowY = listY.get(0);
			listX.remove((int)0);
			listY.remove((int)0);
			
			if(isAllowedXY(nowX+1,nowY))
			{
				if(_AStarTable[nowY][nowX+1]>_AStarTable[nowY][nowX]+1)
				{
					if(!_blockMap[nowY][nowX+1])
					{
					_AStarTable[nowY][nowX+1] = _AStarTable[nowY][nowX] + 1;
					listX.add(nowX+1);
					listY.add(nowY);
					}	
				}
			}
			
			if(isAllowedXY(nowX-1,nowY))
			{
				if(_AStarTable[nowY][nowX-1]>_AStarTable[nowY][nowX]+1)
				{
					if(!_blockMap[nowY][nowX-1])
					{
					_AStarTable[nowY][nowX-1] = _AStarTable[nowY][nowX] + 1;
					listX.add(nowX-1);
					listY.add(nowY);
					}
				}
			}
			
			if(isAllowedXY(nowX,nowY+1))
			{
				if(_AStarTable[nowY+1][nowX]>_AStarTable[nowY][nowX]+1)
				{
					if(!_blockMap[nowY+1][nowX])
					{
					_AStarTable[nowY+1][nowX] = _AStarTable[nowY][nowX] + 1;
					listX.add(nowX);
					listY.add(nowY+1);
					}
				}
			}
			
			if(isAllowedXY(nowX,nowY-1))
			{
				if(_AStarTable[nowY-1][nowX]>_AStarTable[nowY][nowX]+1)
				{
					if(!_blockMap[nowY-1][nowX])
					{
					_AStarTable[nowY-1][nowX] = _AStarTable[nowY][nowX] + 1;
					listX.add(nowX);
					listY.add(nowY-1);
					}
				}
			}
		}
	}
	
	public boolean isAllowedXY(int X,int Y)
	{
		if(X>=0&&X<GetSizeX())
		{
			if(Y>=0&&Y<GetSizeY())
			{
				return true;
			}
		}
		
		return false;
	}
	
	
	
	
	
	public GameMap(GameLevel level,int[][] customMapData,final MapMainCharacter mainCharacter)
	{
		_level = level;
		_mapData = customMapData;
		
		int x;
		int y;
		x = GetSizeX();
		y = GetSizeY();
		
		Log.d("gameloop", x+":"+y);
		
		_sprite = new Sprite [y][x];
		_blockMap = new boolean[y][x];
		_AStarTable = new int[y][x];
		
		for(int i=0;i<x;i++)
			for(int j=0;j<y;j++)
			{
				_AStarTable[j][i]=-1;
				_blockMap[j][i]=false;
				
				

				int blockIndex = customMapData[j][i];
				Sprite block = null;
				
				String blockPic = MapBlockDictionary[blockIndex]._picString;
				_blockMap[j][i] = MapBlockDictionary[blockIndex]._isBlock;
				
				_sprite[j][i] = block = new MapSprite(0,0,_level._resoruceManager.IntelligentLoadTextureRegion(blockPic),i,j){
					@Override
					public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) 
					{
							if(_mapClicked!=null)
							_mapClicked.MapCoordinateClicked(this.positionX,this.positionY);
							
							mainCharacter._characterMoveListener = null;
						return true;
					}
				};
				
				_level._sceneManager.attach(block);
				//_level._sceneManager.registerTouchArea(block);
				
				//block.setCurrentTileIndex(MapBlockDictionary[blockIndex]._picIndex);
				block.setPosition(32*i+8,32*j+8);
				block.setScale(2,2);
				
			}
	}
	

	
	public interface MapClickedEventHandler
	{
		public void MapCoordinateClicked(int X,int Y);
	}
	
	
}
